package top.gdonly.signin.sevelets;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;
import org.json.JSONException;
import org.json.JSONObject;

import top.gdonly.signin.mapper.UserMapper;
import top.gdonly.signin.po.User;
import utils.SqlSessionFactoryUtil;

/**
 * Servlet implementation class GetActualSigned
 */
@WebServlet("/GetActualSigned")
public class GetActualSigned extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetActualSigned() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String classId = request.getParameter("class_id");
		SqlSession session = SqlSessionFactoryUtil.openSession();
		UserMapper userMapper = session.getMapper(UserMapper.class);
		List<User> users = userMapper.getActualSigned(classId);
		JSONObject object = new JSONObject();
		try {
			if(users.size() > 0){
				object.put("actual", users.size());
			}
			else{
				object.put("actual", -1);
			}
		} catch (JSONException e) {
			e.printStackTrace();
			
		}
		response.getWriter().append(object.toString());
	}

}
